import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '设备编号',		name: 'gps_number' },
			{ label: '设备状态',		name: 'device_status',	type: 'select', loading:true },
		],
		// 提交表单列表
		formData : [
			{ label: '设备编号',		name: 'gps_number', 		required:true, half:true },
			{ label: '步经参数',		name: 'step_param', 		required:true, half:true },
			{ label: '设备状态',		name: 'device_status', 		required:true, half:true, type:'select' },
			{ label: '排序',			name: 'sort', 				half:true, required:true, type:'number'  },
			{ label: 'IMEI',		name: 'gps_unique', 		half:true, required:true },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 设备状态
			$fn.setRows( [ forms, formData ], 'device_status', await f.getDisCache( { code:200 } ))
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'sp-gps-device/index')
	
	// 表单
	cols = [
		{ title: '设备编号', 	dataIndex: 'gps_number', 			width:200 },
		{ title: '步经参数', 	dataIndex: 'step_param', 			width:200 },
		{ title: '设备状态', 	dataIndex: 'device_status_name', 	...$fn.table.status },
		{ title: '创建时间', 	dataIndex: 'created_at', 			...$fn.table.time },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'sp-gps-device/info', { uuid }),				// 编辑
			this.onDel([ uuid ]),															// 删除
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this),			// 添加
		this.onDel(),						// 删除
	]
	// 删除
	onDel = uuid => Delete.call(this, 'sp-gps-device/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='GPS 设备列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'sp-gps-device/add', editApi:'sp-gps-device/edit' }) }
			</>
		)
	}
}

export default Index